package com.tsing.cli.common.config;

import com.tsing.cli.common.dto.CustomUserDetails;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Set;

/**
 * 自定义权限校验方法
 *
 * @author TheTsing
 */
@Service("ex")
public class SecurityExpressionConfig {

    public boolean check(String... authorities) {
        CustomUserDetails customUserDetails = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Set<String> permissions = customUserDetails.getPermissions();
        return Arrays.stream(authorities).anyMatch(permissions::contains);
    }

}
